<template>
  <div class="index">
    <!-- 顶部 -->
    <div class="bar">
      <div class="left init"></div>
      <div class="middle" v-show="!$route.meta.gender">
        <button
          :class="{ active: $store.state.ismale == 'male' }"
          @click="active('male')"
        >
          男生
        </button>
        <button
          :class="{ active: $store.state.ismale == 'female' }"
          @click="active('female')"
        >
          女生
        </button>
      </div>
      <div class="right">
        <div>
          <span class="iconfont icon-icon-12" @click="toRouter('user')"></span>
          <span
            class="iconfont icon-shujia"
            @click="toRouter('bookshelf')"
          ></span>
        </div>
      </div>
    </div>
    <!-- 轮播图 -->
    <div class="banner">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="#ed424b">
        <van-swipe-item>
          <img src="../assets/images/banner01.jpg" />
        </van-swipe-item>
        <van-swipe-item>
          <img src="../assets/images/banner02.jpg" />
        </van-swipe-item>
        <van-swipe-item>
          <img src="../assets/images/banner03.jpg" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!-- 搜索框 -->
    <div class="search" @click="toRouter('search')">
      <div>
        <span class="iconfont icon-icon-7">大王饶命</span>
      </div>
    </div>
    <!-- 图标导航栏 -->
    <div class="navs">
      <div class="ul">
        <li @click="toRouter('category')">
          <span class="category"></span> 分类
        </li>
        <li @click="toRouter('rank')"><span class="rank"></span> 排行榜</li>
        <li @click="toRouter('free')"><span class="free"></span> 免费</li>
        <li @click="toRouter('finished')"><span class="finish"></span> 完本</li>
        <li @click="toRouter('god')"><span class="dashen"></span> 大神</li>
      </div>
    </div>
    <!-- 热门小说 -->
    <book-item :data="hotData" class="margin">
      <h3>
        热门小说 <span>起点编辑推荐</span>
        <span class="load" v-if="!hotData.male"
          ><van-loading type="spinner" size="0.5rem"
        /></span>
      </h3>
    </book-item>
    <!-- 限时免费 -->
    <book-item :data="hotData" :time="time" class="margin">
      <h3>
        限时免费
        <div class="timeB">
          <van-count-down :time="time" format="DD:HH:mm:ss:SS">
            <template #default="timeData">
              <span class="block days">{{ timeData.days | timeformat }}</span
              >:<span class="block">{{ timeData.hours | timeformat }}</span
              >:<span class="block">{{ timeData.minutes | timeformat }}</span
              >:<span class="block">{{ timeData.seconds | timeformat }}</span>
            </template>
          </van-count-down>
        </div>
      </h3>
      <div class="more" @click="toRouter('free')">
        更多<span class="iconfont icon-arrow-right-"></span>
      </div>
      <template v-slot:em>
        <em>限免</em>
      </template>
    </book-item>
    <!-- 排行榜 -->
    <book-rank :data="hotData" :time="time" :show="true" class="margin">
      <h3>排行榜</h3>
      <div class="more" @click="toRouter('rank')">
        更多<span class="iconfont icon-arrow-right-"></span>
      </div>
      <!-- tab -->
      <template v-slot:tab>
        <div class="rank_navs">
          <li @click="rank=0" :class="{active:rank == 0}">热销榜</li>
          <li @click="rank=1" :class="{active:rank == 1}">风云榜</li>
          <li @click="rank=2" :class="{active:rank == 2}">签约榜</li>
          <li @click="rank=3" :class="{active:rank == 3}">推荐榜</li>
        </div>
      </template>
    </book-rank>
    <!-- 广告位 -->
    <div class="poster">
      <img src="../assets/images/banner01.jpg" alt="" />
      <span>广告</span>
    </div>
    <!-- 新书抢鲜 -->
    <book-list :data="temporary" class="margin">
      <template v-slot:header>
        <h3>新书抢鲜 <span>24小时热销新书</span></h3>
        <div class="more">
          更多<span class="iconfont icon-arrow-right-"></span>
        </div>
      </template>
    </book-list>
    <!-- 畅销完本 -->
    <book-list :data="temporary" class="margin">
      <template v-slot:header>
        <h3>畅销完本 <span>今日畅销完本书</span></h3>
        <div class="more">
          更多<span class="iconfont icon-arrow-right-"></span>
        </div>
      </template>
    </book-list>
    <!-- 分类推荐 -->
    <book-rank :data="hotData" :time="time" class="margin">
      <h3>分类推荐 <span> 频道主编推荐</span></h3>
      <div class="more" @click="toRouter('category')">
        更多<span class="iconfont icon-arrow-right-"></span>
      </div>
      <!-- tab -->
      <template v-slot:tab>
        <div class="rank_navs">
          <li class="active">玄幻奇幻</li>
          <li>武侠仙侠</li>
          <li>都市职场</li>
        </div>
      </template>
    </book-rank>
    <book-rank :data="hotData" :time="time" :show="false">
      <!-- tab -->
      <template v-slot:tab>
        <div class="rank_navs">
          <li class="active">历史军事</li>
          <li>游戏体育</li>
          <li>科幻悬疑</li>
        </div>
      </template>
    </book-rank>
    <!-- 轻小说 -->
    <book-item :data="hotData" class="margin">
      <h3>轻小说</h3>
      <div class="more">
        更多<span class="iconfont icon-arrow-right-"></span>
      </div>
    </book-item>
    <!-- 猜你喜欢 -->
    <!-- 精选专题 -->
    <div class="sel">
      <div class="header">
        <h3>精选专题</h3>
      </div>
      <div class="content">
        <div class="as">
          <a
            href="https://creditcard.ecitic.com/h5/shenqing/qidian/index.html?qd_dd_p1=30913"
          >
            <img src="../assets/images/sel01.jpg" alt="" />
          </a>
          <a
            href="https://aweme.snssdk.com/magic/page/ejs/5e999825623758030ecabdad?appType=douyin&qd_dd_p1=56972"
          >
            <img src="../assets/images/sel02.jpg" alt="" />
          </a>
          <a href="https://chat.yuewen.com/?qd_dd_p1=7471">
            <img src="../assets/images/sel03.jpg" alt="" />
          </a>
          <a
            href="https://i-game.qidian.com/Qidian/Location/AdProp?id=334784&pt=1&qd_dd_p1=83336"
          >
            <img src="../assets/images/sel04.jpg" alt="" />
          </a>
        </div>
      </div>
      <div class="loginTips" v-if="!$store.state.login">
        登录后获得更多特色功能<i>·</i><a href="javascript:;">立即登录</a>
      </div>
    </div>
  </div>
</template>

<script>
import { getCategories } from "../api/index/allIndex.js";
export default {
  data() {
    return {
      time: 2 * 24 * 60 * 60 * 1000,
      hotData: {
        female: null,
        male: null,
      },
      temporary: [
        {
          _id: "5d020a2e035a4416960fd2f1",
          author: "绝人",
          cover:
            "/agent/http%3A%2F%2Fimg.1391.com%2Fapi%2Fv1%2Fbookcenter%2Fcover%2F1%2F3387088%2F3387088_368c1a79dccf420d8a343009fd954d29.jpg%2F",
          majorCate: "都市",
          shortIntro:
            "“我叫韩三千”\r\n三年前，我如同一条狗一样，被赶出家族的大门\r\n三年后，家族掌门却来求我，让我回家主持大局……\r\n\r\n入赘三年，我可以忍受岳母的欺辱，朋友的嘲笑，但我忍不了你受一丁点委屈\r\n隐忍三年，我可以承担废物的骂名，家族的唾弃，但我受不了你哪怕一滴眼泪\r\n所有人都以为可以骑在我头上，而我，只等你牵起我的手，便给你整个世界。",
          title: "超级女婿",
        },
        {
          _id: "5d020a2e035a4416960fd2f1",
          author: "绝人",
          cover:
            "/agent/http%3A%2F%2Fimg.1391.com%2Fapi%2Fv1%2Fbookcenter%2Fcover%2F1%2F3387088%2F3387088_368c1a79dccf420d8a343009fd954d29.jpg%2F",
          majorCate: "都市",
          shortIntro:
            "“我叫韩三千”\r\n三年前，我如同一条狗一样，被赶出家族的大门\r\n三年后，家族掌门却来求我，让我回家主持大局……\r\n\r\n入赘三年，我可以忍受岳母的欺辱，朋友的嘲笑，但我忍不了你受一丁点委屈\r\n隐忍三年，我可以承担废物的骂名，家族的唾弃，但我受不了你哪怕一滴眼泪\r\n所有人都以为可以骑在我头上，而我，只等你牵起我的手，便给你整个世界。",
          title: "超级女婿",
        },
        {
          _id: "5d020a2e035a4416960fd2f1",
          author: "绝人",
          cover:
            "/agent/http%3A%2F%2Fimg.1391.com%2Fapi%2Fv1%2Fbookcenter%2Fcover%2F1%2F3387088%2F3387088_368c1a79dccf420d8a343009fd954d29.jpg%2F",
          majorCate: "都市",
          shortIntro:
            "“我叫韩三千”\r\n三年前，我如同一条狗一样，被赶出家族的大门\r\n三年后，家族掌门却来求我，让我回家主持大局……\r\n\r\n入赘三年，我可以忍受岳母的欺辱，朋友的嘲笑，但我忍不了你受一丁点委屈\r\n隐忍三年，我可以承担废物的骂名，家族的唾弃，但我受不了你哪怕一滴眼泪\r\n所有人都以为可以骑在我头上，而我，只等你牵起我的手，便给你整个世界。",
          title: "超级女婿",
        },
      ], // 静态数据 新书抢鲜
      rank: 0,
    };
  },
  methods: {
    active(flag) {
      this.$store.commit("setIsmale", flag);
    },
    toRouter(path, params) {
      this.$router.push({
        path: `/${path}`,
        query: { ...params },
      });
    },
    gotoDetails(path, params) {
      this.$router.push({
        path: `/${path}`,
        query: { ...params },
      });
    },
    getData() {
      let q = {
        gender: "male",
        type: "hot",
        major: "玄幻",
        limit: 5,
      };
      getCategories(q).then((data) => {
        if (data.ok) {
          console.log("热门小说获取成功");
          this.hotData.male = data.books;
        }
      });
      q.gender = "female";
      q.major = "古代言情";
      getCategories(q).then((data) => {
        if (data.ok) {
          this.hotData.female = data.books;
        }
      });
    },
  },
  created() {
    this.$nextTick(() => {
      this.getData();
    });
  },
  filters: {
    timeformat: function (old) {
      return old < 10 ? "0" + old : old;
    },
    imgUrl(url) {
      return "http://statics.zhuishushenqi.com" + url;
    },
  },
  computed: {
    gender() {
      return this.$store.state.ismale;
    },
  },
};
</script>
<style lang="less" scoped>
// 轮播图
.bar {
  height: 0.7rem;
  display: flex;
  justify-content: space-between;
  padding: 0.25rem;
  .left {
    width: 2.4rem;
    margin-left: 0.15625rem;
    flex: 1;
    &.init {
      background: url("../assets/images/logo-qdm.02fc8.svg") no-repeat center;
      background-size: 100%;
    }
    &.navname {
      font-size: 0;
      div {
        height: 100%;
        line-height: 0.7rem;
        font-size: 0.45rem;
        em {
          font-style: normal;
          font-size: 0.38rem;
          margin-left: 0.304375rem;
          vertical-align: top;
          line-height: 0.65rem;
        }
      }
    }
  }
  .middle {
    font-size: 0;
    flex: 2;
    text-align: center;
    button {
      font-size: 0.4rem;
      border: 1px solid #ed424b;
      color: #ed424b;
      background-color: #f6f7f9;
      padding: 0.048125rem 0.3125rem;
      height: 100%;
      &:nth-child(1) {
        border-radius: 4px 0 0 4px;
      }
      &:nth-child(2) {
        border-radius: 0 4px 4px 0;
      }
      &.active {
        background-color: #ed424b;
        color: white;
      }
    }
  }
  .right {
    font-size: 0;
    flex: 0 0 2.28125rem;
    span {
      position: relative;
      font-size: 0.625rem;
      color: #ed424b;
      margin-left: 0.46875rem;
      &:nth-child(2) {
        font-size: 0.5rem;
      }
    }
  }
}
// 轮播图
.banner {
  width: 92%;
  margin: 0.15625rem auto 0;
  img {
    width: 100%;
    height: auto;
  }
  // 指示器位置
  .van-swipe__indicators {
    bottom: 15px;
  }
  // 指示器颜色
  .van-swipe__indicator {
    opacity: 1;
    background-color: white;
  }
}
// 搜索
.search {
  width: 100%;
  div {
    width: 92%;
    height: 0.859375rem;
    margin: 0 auto;
    text-align: center;
    font-size: 0;
    background-color: white;
    border-radius: 4px;
    span {
      display: inline-block;
      height: 100%;
      color: #999;
      font-size: 0.4rem;
      line-height: 0.859375rem;
    }
  }
}
// 图标导航
.navs {
  margin-top: 6px;
  background-color: white;
  padding: 0.578125rem 0;
  .ul {
    display: flex;
    span {
      width: 26px;
      height: 26px;
      background-color: skyblue;
      display: block;
      margin: 0 auto 6px;
      &.category {
        background: url("../assets/images/sprite1.png");
        background-size: 87px 87px;
        background-position: -61px -28px;
      }
      &.rank {
        background: url("../assets/images/sprite1.png");
        background-size: 87px 87px;
        background-position: 0 0;
      }
      &.free {
        background: url("../assets/images/sprite1.png");
        background-size: 87px 87px;
        background-position: 0 -30px;
      }
      &.finish {
        background: url("../assets/images/sprite1.png");
        background-size: 87px 87px;
        background-position: 0 -60px;
      }
      &.dashen {
        background: url("../assets/images/sprite1.png");
        background-size: 87px 87px;
        background-position: -30px -30px;
      }
    }
    li {
      width: 25%;
      font-size: 0.359375rem;
      font-weight: 600;
      color: #555;
      text-align: center;
    }
  }
}
// 广告位
.poster {
  width: 100%;
  padding: 0.3125rem;
  box-sizing: border-box;
  position: relative;
  img {
    width: 100%;
    height: auto;
    background-color: tomato;
  }
  span {
    position: absolute;
    bottom: 0;
    right: 0.3125rem;
    font-size: 0.3125rem;
    background-color: rgba(0, 0, 0, 0.3);
    color: white;
    padding: 1px;
  }
}
// 精选专题
.sel {
  padding: 0.46875rem;
  padding-bottom: 0;
  background-color: white;
  margin-top: 15px;
  padding-bottom: 0.625rem;
  border-bottom: 1px solid #ddd;
  .header {
    border-left: 2.5px solid #ed424b;
    text-indent: 8px;
    h3 {
      font-family: "宋体";
      font-weight: 600;
      font-size: 0.390625rem;
      position: relative;
      span {
        font-size: 0.34375rem;
        color: #777;
        font-weight: normal;
        vertical-align: botto;
      }
    }
  }
  .content {
    margin-top: 0.46875rem;
    & > .as {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      a {
        width: 48%;
        margin-bottom: 0.15625rem;
        img {
          width: 100%;
          height: auto;
        }
      }
    }
  }
}
// 时间样式
.timeB {
  // 时间样式
  text-indent: 0;
  display: inline-block;
  .block {
    display: inline-block;
    color: #fff;
    background-color: black;
    text-indent: 0px;
    line-height: initial;
    padding: 1px;
    border-radius: 1px;
    &.days {
      background-color: #ee0a24;
    }
  }
}
// 旋转动画
@keyframes loadAM {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>